<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/7 0007
 * Time: 11:20
 */

namespace app\currency\controller;

use app\currency\service\PhpSpreadsheetService;
use library\Controller;
use think\Db;

/**
 * 阿姨提现管理
 * Class CleanerWithdrawal
 * @package app\currency\controller
 */
class CleanerWithdrawal extends Controller
{

    protected $table = 'CleanerWithdrawal';

    /**
     * 提现列表
     * @menu true
     * @auth true
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '申请列表';
        $query = $this->_query($this->table)
            ->equal('status,pay_state')
            ->dateBetween('create_at')
            ->order('status asc,pay_state asc,create_at desc');
        $action = $this->request->param('action','');
        if($action == 'export'){
            $list = $query
                ->page(false,false);
            $this->export($list['list']);
        } else {
            $query->page();
        }
    }
    /**
     * 订单列表导出到表格
     * @param $list
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    protected function export($list)
    {
        $fileName='提现列表_'.date('Y-m-d H:i:s');
        $heads = [
            [
                'title' => '姓名',
                'width' => '15',
                'field' => 'cleaner_name'
            ],
            [
                'title' => '电话',
                'width' => '20',
                'field' => 'cleaner_phone'
            ],
            [
                'title' => '提现金额',
                'width' => '20',
                'field' => 'amount'
            ],
            [
                'title' => '申请时间',
                'width' => '25',
                'field' => 'create_at'
            ],
            [
                'title' => '银行卡号',
                'width' => '25',
                'field' => 'card_no'
            ],
            [
                'title' => '银行名称',
                'width' => '25',
                'field' => 'bank_name'
            ],
            [
                'title' => '开户支行',
                'width' => '30',
                'field' => 'bank_open_branch_name'
            ],
            [
                'title' => '审核状态',
                'width' => '20',
                'field' => 'status_txt'
            ],[
                'title' => '打款状态',
                'width' => '20',
                'field' => 'pay_state_txt'
            ]
        ];
        PhpSpreadsheetService::export($fileName,$heads,$list);
    }
    protected function _index_page_filter(&$data)
    {
        $cids = array_unique(array_column($data, 'cid'));
        $cleanerList = Db::name('Cleaner')->whereIn('id', $cids)->select();
        $bids = array_unique(array_column($data,'bid'));
        $bank_list = Db::name('CleanerBankCard')->whereIn('id',$bids)->select();
        foreach ($data as &$vo) {
            $vo['cleaner'] = [];
            $vo['bank'] = [];
            list($vo['cleaner_name'],$vo['cleaner_phone'],$vo['card_no'],$vo['bank_name'],$vo['bank_open_branch_name']) = [
                '','','','',''
            ];
            foreach ($cleanerList as $cleaner) if ($cleaner['id'] === $vo['cid']) {
                $vo['cleaner'] = $cleaner;
                $vo['cleaner_name'] = $cleaner['name'];
                $vo['cleaner_phone'] = $cleaner['phone'];
            }
            foreach ($bank_list as $bank) if ($bank['id'] === $vo['bid']) {
                $vo['bank'] = $bank;
                $vo['card_no'] = $bank['card_no'];
                $vo['bank_name'] = $bank['bank_name'];
                $vo['bank_open_branch_name'] = $bank['bank_open_branch_name'];
            }
            $vo['status_txt'] = $vo['status'] == -1 ? '审核失败': ($vo['status'] == 0? '待审核' : '审核通过');
            $vo['pay_state_txt'] = $vo['pay_state'] ? '已打款' : '未打款';
        }
    }

    /**
     * 驳回提现请求
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function forbid()
    {
        $this->applyCsrfToken();
        $id = $this->request->post('id');
        $withdrawal = Db::name($this->table)->where([['id','eq',$id],['status','eq','0']])->find();
        Db::transaction(function() use($withdrawal){
            Db::name('Cleaner')->where('id','eq',$withdrawal['cid'])->setInc('balance',$withdrawal['amount']);
            Db::name($this->table)->where('id','eq',$withdrawal['id'])->setField('status','-1');
            Db::name('CleanerBalanceLog')->insert([
                'cid' => $withdrawal['cid'],
                'amount' => $withdrawal['amount'],
                'content' => "提现申请被驳回，余额{$withdrawal['amount']}元已返回到账户中！"
            ]);
        });
        $this->success('操作成功！');
    }

    /**
     * 通过请求
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '1']);
    }

    /**
     * 打款
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function save(){
        $this->applyCsrfToken();
        $this->_save($this->table);
    }
}